venv dist-packages site-packages
Source
-
by default, python will only have access to the
site-packages
directly installed inside a venv -
if we want to access the packages from the (Linux) system’s
dist-packages
that were installed using the system’s package manager, there are multiple possible ways- set
PYTHONPATH
to include/usr/lib/python<VERSION>/dist-packages
this has the drawback that now thedist-packages
take precedence over thesite-packages
from the venv, thus if we have the same package installed in the venv but with a different (usually more recent) version than in thedist-packages
, python will use the one from thedist-packages
, which is usually not what we want - create a
.pth
file in the venv’ssite-packages
folder that points to the system’sdist-packages
this has the advantage, that the venv’s packages will be used first and only if python cannot find a package in the venv, it will go looking into the system’sdist-packages
echo "/usr/lib/python<VERSION>/dist-packages" > .venv/lib/python<VERSION>/site-packages/dist-packages.pth
(the
<VERSION>
has to match, of course, so for Python 3.10, for example, this would readpython3.10
in both cases) - set